package com.newtouch.bxzs.syslog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.LogTransaction;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import com.newtouch.bxzs.syslog.mapper.LogTransactionMapper;
import com.newtouch.bxzs.syslog.model.bo.LogTransactionBO;
import com.newtouch.bxzs.syslog.model.vo.LogTransactionVO;
import com.newtouch.bxzs.syslog.service.LogTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 交易日志service 实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020年12月18日</td>
 * <td>renyi</td>
 * <td>Update</td>
 * </tr>
 * </table>
 *
 * @author renyi
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class LogTransactionServiceImpl extends ServiceImpl<LogTransactionMapper, LogTransaction>
		implements LogTransactionService {

	/** 日期格式化 */
	private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	@Autowired
	private LogTransactionMapper logTransactionMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.syslog.service.LogTransactionService#
	 * selectLogTransactionList(com.newtouch.bxzs.syslog.model.vo.
	 * LogTransactionVO)
	 */
	@Override
	public IPage<LogTransactionBO> selectLogTransactionList(LogTransactionVO logTransactionVO) {
		QueryWrapper<LogTransaction> wrapper = new QueryWrapper<>();
		/** 交易时间对应的数据库字段 */
		String createTimeCloumn = "USER_OPERATION_TIME";
		// 判断用户是否输入查询条件
		if (logTransactionVO.getStartTime() == null && logTransactionVO.getEndTime() == null) {
			// 默认查询近七天
			Date datetime = DateUtil.beginOfDay(DateUtil.lastWeek());
			// 设置查询条件
			wrapper.ge(createTimeCloumn, datetime);
		}
		// 根据条件查询
		// 当开始时间不为空
		if (logTransactionVO.getStartTime() != null) {
			wrapper.ge(createTimeCloumn, DateUtil.format(logTransactionVO.getStartTime(), DATE_FORMAT));
		}
		// 当结束时间不为空
		if (logTransactionVO.getEndTime() != null) {
			wrapper.le(createTimeCloumn, DateUtil.format(logTransactionVO.getEndTime(), DATE_FORMAT));
		}
		// 判断交易状态是否为空
		if (logTransactionVO.getIsSuccess() != null) {
			wrapper.eq("IS_SUCCESS", logTransactionVO.getIsSuccess());
		}
		// 判断交易类型是否为空
		if (logTransactionVO.getUserOperationType() != null) {
			wrapper.eq("USER_OPERATION_TYPE", logTransactionVO.getUserOperationType());
		}

		// 按交易时间降序排列
		wrapper.orderByDesc(createTimeCloumn);
		// 分页查询
		Page<LogTransaction> logTransactionPage = new Page<>(logTransactionVO.getCurrentPage(),
				logTransactionVO.getPageSize());
		logTransactionPage = logTransactionMapper.selectPage(logTransactionPage, wrapper);
		if (BeanUtil.isEmpty(logTransactionPage) || CollectionUtil.isEmpty(logTransactionPage.getRecords())) {
			return null;
		}
		// 将LogError 转为LogErrorBO
		List<LogTransactionBO> logTransactionBOList = EntityToBeanUtils.copyBean(logTransactionPage.getRecords(),
				LogTransactionBO.class);
		// 设置查询 LogError 分页返回结果
		Page<LogTransactionBO> logTransactionBOPage = new Page<>(logTransactionPage.getCurrent(),
				logTransactionPage.getSize(), logTransactionPage.getTotal());
		logTransactionBOPage.setPages(logTransactionPage.getPages());
		logTransactionBOPage.setRecords(logTransactionBOList);
		return logTransactionBOPage;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.syslog.service.LogTransactionService#
	 * getLogTransactionById(java.lang.String)
	 */
	@Override
	public LogTransactionBO getLogTransactionDetail(String transactionLogId) {
		if (StringUtils.isEmpty(transactionLogId)) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		return logTransactionMapper.getLogTransactionDetail(transactionLogId);
	}
}
